Imports System.Data

Public Class WebControls_Survey
    Inherits System.Web.UI.UserControl

    Private mSeq_Cd As String
    Private mCID As String
    Private mRiskProfileCode As String

    Public Property Seq_CD() As String
        Get
            Seq_CD = mSeq_Cd
        End Get
        Set(ByVal value As String)
            mSeq_Cd = value
        End Set
    End Property

    Public Property Client_ID() As String
        Get
            Client_ID = mCID
        End Get
        Set(ByVal value As String)
            mCID = value
        End Set
    End Property

    Public Property Risk_Code() As String
        Get
            Risk_Code = mRiskProfileCode
        End Get
        Set(ByVal value As String)
            mRiskProfileCode = value
        End Set
    End Property

    Public Overrides Sub Databind()
        Dim dao As New DAOMain.RISK_PROFILE_DTL_DAO
        Dim vo As New DAOMain.RISK_PROFILE_DTL_VO

        vo.RISK_PROFILE_CD = mRiskProfileCode
        Dim dt As DataTable = dao.Search(vo)

        DataList.DataSource = dt
        DataList.DataBind()

        dao = Nothing
        vo = Nothing
    End Sub

    Public Function Update() As Boolean
        Dim blnsuccess As Boolean = False

        If Delete() Then
            Dim dli As DataListItem

            For Each dli In DataList.Items
                Dim ch As CheckBox = dli.FindControl("ch")
                Dim lbl As Label = dli.FindControl("lbl")

                If Not IsNothing(ch) And Not IsNothing(lbl) Then
                    If ch.Checked Then
                        Dim dao As New DAOMain.POLICY_CLIENT_RISK_DAO
                        Dim vo As New DAOMain.POLICY_CLIENT_RISK_VO

                        With vo
                            .POLICY_CLIENT_CD = mCID
                            .POLICY_SEQ_CD = mSeq_Cd
                            .RISK_DTL_CD = lbl.Text
                            .RISK_PROFILE_CD = mRiskProfileCode
                            .POLICY_CLIENT_RISK_CREA_BY = Session("Username")
                            .POLICY_CLIENT_RISK_CREA_DT = Now
                        End With

                        If dao.Create_Record(vo) Then
                            blnsuccess = True
                        End If
                    End If
                    
                End If
            Next
        End If
    End Function

    Private Function Delete() As Boolean
        Dim dao As New DAOMain.POLICY_CLIENT_RISK_DAO
        Dim vo As New DAOMain.POLICY_CLIENT_RISK_VO
        Dim blnSuccess As Boolean = False

        vo.RISK_PROFILE_CD = mRiskProfileCode
        vo.POLICY_SEQ_CD = mSeq_Cd
        vo.POLICY_CLIENT_CD = mCID

        If dao.Delete(vo) Then
            blnSuccess = True
        End If

        Return blnSuccess
    End Function

    Public Sub Disable()
        Dim dli As DataListItem

        For Each dli In Me.DataList.Items
            Dim ch As CheckBox = dli.FindControl("ch")

            ch.Enabled = False
        Next
    End Sub

    Protected Sub DataList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList.ItemDataBound
        With e.Item
            Dim mlbl As Label = .FindControl("lbl")
            ''Dim plbl As Label = .FindControl("lblProfile")
            Dim ch As CheckBox = .FindControl("ch")

            ''plbl.Text = .DataItem("risk_profile_cd").ToString
            'mlbl.Text = .DataItem("risk_dtl_cd").ToString
            'ch.Text = .DataItem("risk_dtl_desc").ToString

            ch.Checked = GetChecked(mlbl.Text)
        End With
    End Sub

    Private Function GetChecked(ByVal Risk_Dtl_CD As String) As Boolean
        Dim dao As New DAOMain.POLICY_CLIENT_RISK_DAO
        Dim vo As New DAOMain.POLICY_CLIENT_RISK_VO
        Dim blnSuccess As Boolean = False

        vo.RISK_DTL_CD = Risk_Dtl_CD
        vo.RISK_PROFILE_CD = mRiskProfileCode
        vo.POLICY_SEQ_CD = mSeq_Cd
        vo.POLICY_CLIENT_CD = mCID

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            blnSuccess = True
        End If

        Return blnSuccess
    End Function

End Class
